﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KC.ExtensionMethods;
using enc = KC.Business.Text.Encoding;
using BLL = KC.Business.Web.MV8;
using DAL = KC.DataAccess.MV;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
		PopulateTopPosts();
		PopulateTopPics();
    }

	private void PopulateTopPosts()
	{
		int ItemsPerPage = int.Parse(System.Configuration.ConfigurationManager.AppSettings["ItemsPerPage"]);
		rPosts.DataSource = BLL.GetTopPosts(ItemsPerPage);
		rPosts.DataBind();
	}
	private void PopulateTopPics()
	{
		int ItemsPerPage = int.Parse(System.Configuration.ConfigurationManager.AppSettings["ItemsPerPage"]);
		rPics.DataSource = BLL.GetTopPics(ItemsPerPage);
		rPics.DataBind();
	}

	protected void rPosts_ItemDataBound(object sender, RepeaterItemEventArgs e)
	{
		if (e.Item == null || e.Item.DataItem == null) return;
		DAL.Post p = (DAL.Post)e.Item.DataItem;

		HyperLink hlTitle = (HyperLink)e.Item.FindControl("hlTitle");
		hlTitle.Text = p.Title;
		hlTitle.NavigateUrl = string.IsNullOrEmpty(p.FriendlyName)
			? "Post.aspx?ID=" + p.ID.ToString()
			: "Post.aspx?Title=" + p.FriendlyName;
		hlTitle.ToolTip =
			p.Title + "\n" +
			"Posted " + p.Created.ToString("MM/dd/yy HH:mm:ss") + " MT \n" +
			"Post Id " + p.ID.ToString() + "\n" +
			p.Body.Length.ToString("###,###,###") + " bytes";
		hlTitle.Attributes.Remove("rel");
		hlTitle.Attributes.Add("rel", "canonical");

		Label lblBody = (Label)e.Item.FindControl("lblBody");
		lblBody.Text = enc.ExtractExcerpt(enc.BlogDecodeForRendering(string.IsNullOrEmpty(p.Description) ? p.Body : p.Description));
		if (!string.IsNullOrEmpty(lblBody.Text)) lblBody.Text = " : " + lblBody.Text + "<br />\n";
	}
	protected void rPics_ItemDataBound(object sender, RepeaterItemEventArgs e)
	{
		if (e.Item == null || e.Item.DataItem == null) return;
		DAL.Gallery g = (DAL.Gallery)e.Item.DataItem;

		HyperLink hlImg = (HyperLink)e.Item.FindControl("hlImg");
		hlImg.NavigateUrl = BLL.MapGallery(g.Filename);
		hlImg.Target = "_blank";
		hlImg.Attributes.Remove("rel");
		hlImg.Attributes.Add("rel", "canonical");

		Image img = (Image)e.Item.FindControl("img");
		img.ImageUrl = BLL.MapGallery(g.ThumbnailFilename);
		img.ToolTip =
			g.DisplayName + "\n" +
			g.Width + " x " + g.Height + "   (" + ((g.Width * g.Height) / 1000000).ToString() + " Megapixels) \n\n" +
			g.Description;
	}

}

